package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Package;
import com.itheima.service.SetmealService;
import com.itheima.util.QiNiuUtil;
import com.itheima.util.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private SetmealDao setmealDao;

    private Logger logger = LoggerFactory.getLogger(SetmealServiceImpl.class);

    /**
     * @return java.util.List<com.itheima.pojo.Package>
     * @author wu_zh
     * @date 2019/7/1 11:53
     * @Description 查询套餐列表
     */
    @Override
    public List<Package> findAll() {
        List<Package> packageList = getPackageListByRedis();
        if (packageList == null || packageList.size() == 0) {
            packageList = setPackageListToRedis();
        }
        return packageList;
    }

    /**
     * @param id
     * @return com.itheima.pojo.Package
     * @author wu_zh
     * @date 2019/7/1 12:37
     * @Description 根据id查询套餐详情
     */
    @Override
    public Package findById(Integer id) {
        Package packagebyId = getPackageByredis(id);
        if (packagebyId == null || "".equals(packagebyId)) {
            packagebyId = setPackageToRedis(id);
        }
        return packagebyId;
    }

    /**
     * @return java.util.List<com.itheima.pojo.Package>
     * @author wu_zh
     * @date 2019/7/1 11:56
     * @Description 从redis中获取套餐列表数据
     */
    private List<Package> getPackageListByRedis() {
        Jedis jedis = jedisPool.getResource();
        if (jedis == null || !jedis.exists("packageList")) {
            logger.debug("redis中套餐列表数据为null");
            return null;
        }
        byte[] data = jedis.get("packageList".getBytes());
        jedis.close();
        logger.debug("返回redis中的套餐列表数据");
        return (List<Package>) SerializeUtil.unserializeList(data);
    }

    /**
     * @param id
     * @return com.itheima.pojo.Package
     * @author wu_zh
     * @date 2019/7/1 12:52
     * @Description 从redis中获取套餐详情数据
     */
    private Package getPackageByredis(Integer id) {
        Jedis jedis = jedisPool.getResource();
        if (jedis == null || !jedis.exists("packagebyId" + id)) {
            logger.debug("redis中套餐详情数据为null");
            return null;
        }
        byte[] data = jedis.get(("packagebyId" + id).getBytes());
        jedis.close();
        logger.debug("返回redis中的套餐详情数据");
        return (Package) SerializeUtil.unserialize(data);
    }

    /**
     * @return java.util.List<com.itheima.pojo.Package>
     * @author wu_zh
     * @date 2019/7/1 11:52
     * @Description 查询数据库, 设置套餐列表数据到redis中
     */
    private List<Package> setPackageListToRedis() {
        Jedis jedis = jedisPool.getResource();
        List<Package> packageList = setmealDao.findAll();
        logger.debug("从MySql中获取套餐列表数据");
        if (packageList == null || packageList.size() == 0) {
            jedis.set("packageList".getBytes(), "".getBytes());
        } else {
            for (Package pkg : packageList) {
                pkg.setImg(QiNiuUtil.DOMAIN + "/" + pkg.getImg());
            }
            jedis.set("packageList".getBytes(), SerializeUtil.serializeList(packageList));
            logger.debug("把套餐列表数据存到redis中");
        }
        return packageList;
    }

    /**
     * @param id
     * @return com.itheima.pojo.Package
     * @author wu_zh
     * @date 2019/7/1 13:00
     * @Description 查询数据库, 设置套餐详情数据到redis中
     */
    private Package setPackageToRedis(Integer id) {
        Jedis jedis = jedisPool.getResource();
        Package packagebyId = setmealDao.findById(id);
        logger.debug("从MySql中获取套餐详情数据");
        if (packagebyId == null || "".equals(packagebyId)) {
            jedis.set(("packagebyId" + id).getBytes(), "".getBytes());
        } else {
            jedis.set(("packagebyId" + id).getBytes(), SerializeUtil.serialize(packagebyId));
            logger.debug("把套餐详情数据存到redis中");
        }
        return packagebyId;
    }
}
